<?php
defined('_JEXEC') or die('Restricted access');

/**
 * Descriptio of Model
 * 
 * @todo 
 * 
 * 
 * @author George Zeakis<zeageorge@google.com>
 * 
 */
class Model extends modelToStorage_adapter{

    protected $_className = NULL;

    /**
     * @var String The Database Table name
     */
    protected $_tableName = NULL;

    /**
     * @var Database The Database Object
     */
    protected $_db = NULL;

    /**
     * @param String $className
     * @param String $tableName 
     */
    function __construct($className, $tableName) {
        parent::__construct($className, $tableName);
        
        $this->_className = $className;
        $this->_tableName = $tableName;

        $this->_db = new Database();
    }

    /**
     * 
     */
    protected function getAll_Records() {
        return $this->_db->select("SELECT * FROM `$this->_tableName`");
    }

    /**
     * @param String $field The column name of a table in a database
     */
    protected function getAllRecords_By($field) {
        return $this->_db->select("SELECT `$field` FROM `$this->_tableName`");
    }

    /**
     *
     * @param String $field
     * @param String $value
     */
    protected function getSingleRecord_By($field, $value) {
        return $this->_db->select("SELECT * FROM `$this->_tableName` WHERE `$field` = :v LIMIT 1", array(':v' => $value));
    }

    /**
     *
     * @param String $field
     * @param String $value
     * @return boolean 
     */
    protected function recordExists($field, $value) {
        if ($this->getSingleRecord_By($field, $value)) {
            return TRUE;
        }
        return FALSE;
    }

    /**
     *
     * @param array $data
     * @return integer
     */
    protected function create($data) {
        return $this->_db->insert($this->_tableName, $data);
    }

    /**
     *
     * @param array $data An associative array
     * @param string $where the WHERE query part
     */
    protected function update($data, $where) {
        return $this->_db->update($this->_tableName, $data, $where);
    }

    /**
     *
     * @param string $where the WHERE query part
     */
    protected function delete_All($where) {
        return $this->_db->delete_all($this->_tableName, $where);
    }

    /**
     *
     * @param string $where the WHERE query part
     */
    protected function delete_SingleRecord($where) {
        return $this->_db->delete($this->_tableName, $where, 1);
    }

}